﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
using Albon.Model;
namespace DAL
{
    public class OrderItemDAL
    {
        /// <summary>
        /// 添加临时订单项
        /// </summary>
        public int addOrderItem(OrderItem orderitem)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@orderid",orderitem.orderID),
                new SqlParameter("@itemid",orderitem.itemID),
                new SqlParameter("@stylelist",orderitem.styleList),
                new SqlParameter("@ret",SqlDbType.Int)
            };
            parameters[3].Direction=ParameterDirection.Output;
           SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_orderitemadd", parameters);
           return int.Parse(parameters[3].Value.ToString());
        }
        /// <summary>
        /// 更新临时订单项信息
        /// </summary>
        public bool updateOrderItem(Albon.Model.OrderItem orderitem)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@ID",orderitem.ID),
                new SqlParameter("@quantity",orderitem.quantity),
                new SqlParameter("@FT",orderitem.FT),
                new SqlParameter("@finished",orderitem.Finished)
            };
            int ret=SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_orderitemupdate", parameters);
            return ret == 1;
        }

        /// <summary>
        /// 根据查询条件 返回Item信息
        /// </summary>
        public DataTable getItem(string limit)
        {
            string cmdstr = "select * from OrderItemView where 1=1 and " + limit;
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
        }
        /// <summary>
        /// 复制订单项
        /// </summary>
        public string copyItem(string olditemid)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@orderitemid",olditemid)
            };
            parameters[0].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, "p_itemcopy", parameters);
            return parameters[0].ToString();
        }

        /// <summary>
        /// 删除一个订单项
        /// </summary>
        /// <param name="orderitemid"></param>
        public void deleteOrderItem(string orderitemid)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@orderitemid",orderitemid)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_orderitemdelete", parameters);
        }
        /// <summary>
        /// 为发货页面获取订单列表中的据有订单项
        /// </summary>
        public DataTable getItemShipView(string idlist)
        {
            string cmdstr = "select * from ItemShipView where orderid in (" + idlist + ")";
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
        }

    }
}
